package com.niit.house.lyl.lift;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;





public class LiftReducer extends Reducer<Text, LiftWritable, Text, Text> {
    int[] SumPrice = {0, 0, 0};

    int[] houses = {0, 0, 0};

    String district = null;



    @Override
    protected void reduce(Text key, Iterable<LiftWritable> values, Context context) throws IOException, InterruptedException {
        district = key.toString();

        for (LiftWritable val : values) {
            if (val.getIsLift() == 1) {
                SumPrice[1] += val.getPrice();
                houses[1]++;
            } else {
                SumPrice[2] += val.getPrice();
                houses[2]++;
            }
            SumPrice[0] += val.getPrice();
            houses[0]++;
        }
    }


    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        context.write(new Text(String.format("%s", district)), new Text(String.format("平均房价: %d, 有电梯: %d, 无电梯: %d", SumPrice[0] / houses[0], SumPrice[1]/houses[1], SumPrice[2]/houses[2])));

    }

}
